import axios from "axios";
import {message} from "antd";

const instance = axios.create({
    baseURL: "/api",
    timeout: 3000,
});

instance.interceptors.request.use(
    function (config) {
        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);

instance.interceptors.response.use(
    function (response) {
        return response;
    },
    function (error) {
        return Promise.reject(error);
    }
);
//导出一个post请求方法
export const post = (url, params) => {
    return instance.post(url, params);
};
export const promise = (url, params) => {
    // url = "http://localhost:8080/api" + url
    // console.log(url, "url")
    return new Promise((resolve, reject) => {
        post(url, params)
            .then(resp => {
                if (resp.data) {
                    const {success = true, message: msg = "系统异常"} = resp.data
                    if (!success) {
                        message.error(msg)
                        return;
                    }
                }
                resolve(resp.data);
                return;
            });
    });
};